package com.google.devrel.example.android.protips.mvp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.devrel.example.android.protips.R;
import com.google.devrel.example.android.protips.adapter.CachedAdapter;
import com.google.devrel.example.android.protips.view.MovingShapeView;

public class MiscPresenter {

	private IMiscView _view;
	private Context _context;

	public MiscPresenter(IMiscView view) {
		_view = view;
		if (view instanceof Context)
			_context = (Context) view;
	}

	public void fillViewContent(final View rootView, final LayoutInflater inflater) {
		BaseAdapter adapter = null;

		adapter = new CachedAdapter(inflater, getData(), R.layout.slow_item);

		ListView listView = (ListView) rootView.findViewById(R.id.listview);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View ivew, int pos,
					long id) {
				ImageView view = new MovingShapeView(inflater.getContext());
				view.setImageResource(R.drawable.android);
				view.setAdjustViewBounds(true);
				view.setLayoutParams(new Gallery.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

				if (rootView instanceof ViewGroup) {
					ViewGroup vg = (ViewGroup) rootView;
					vg.removeAllViews();
					vg.addView(view);
				}
			}
		});
	}
	
	protected List<String> getData() {
    	List<String> data = new ArrayList<String>();
    	for (int i=0; i<100; i++) {
    		data.add("test " + i);
    	}
    	return data;
    }
}
